package dk.flashnotes.fff.helpers.pageInspector {
	import dk.flashnotes.fff.ElementFactory;	import dk.flashnotes.fff.FFFBase;	import dk.flashnotes.fff.page.part.APart;	import dk.flashnotes.fff.page.part.EPartStates;	import dk.flashnotes.fff.stateHolder.StateHolderEvent;		import flash.display.Sprite;		
	/**
	 * @author jk
	 */
	public class PartLabel extends Sprite implements IClearable {
		private var _doPart : APart;
		private var _doLabel : InspectorLabel;
		private var _oColors : Object;

		public function PartLabel(doPart : APart) {
			
			_doPart = doPart;
			setup();
		}
		
		private function setup() : void {
			
			_doPart.addEventListener(StateHolderEvent.etSTATE_CHANGED, onStateChanged, false, 0, true);
			
			_oColors = new Object();
			_oColors[EPartStates.READY_TO_OPEN] = EColorCodes.uintGREY;
			_oColors[EPartStates.OPENING] = EColorCodes.uintYELLOW;
			_oColors[EPartStates.OPENED] = EColorCodes.uintGREEN;
			_oColors[EPartStates.CLOSING] = EColorCodes.uintYELLOW;
			_oColors[EPartStates.CLOSED] = EColorCodes.uintGREY;
			
			_doLabel = new InspectorLabel();
			addChild(_doLabel);
			
			updateLabel();
			_doLabel.setMarker(EColorCodes.uintGREY);

			setState(_doPart.sState);
		}
		
		private function onStateChanged(event : StateHolderEvent) : void {
			if(_doPart.sState) {
				setState(_doPart.sState);
			}
		}

		private function setState(sNewState : String) : void {
			if (_oColors[sNewState]!=null) {
				_doLabel.setMarker(Number(_oColors[sNewState]));
			}
			updateLabel();
		}
		
		private function updateLabel() : void {
			_doLabel.sText = get_sLabelText();
		}
		
		private function get_sLabelText() : String {
			
			var sPartColor:String="#FFFFFF";
			
			if (_doPart is FFFBase.get_oInstance().oElementFactory.get_cDefaultPartType()) {
				sPartColor="#F70000";
			}
			
			return "<font color='" + sPartColor + "'><b>" + _doPart.oPartData.sPartTemplateAlias + "</b></font><font color='#DDDDDD'> : " + _doPart.sState+ "</font>";
		}
		
		public function clear() : void {
			_doPart.removeEventListener(StateHolderEvent.etSTATE_CHANGED, onStateChanged);
			_doLabel=null;
			_doPart=null;
		}
	}
}
